package com.andatsoft.laisim.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.theme.ThemeManager;

public class SlimVerticalProgressBar extends View {

	public interface SlimProgressClicked {
		public void onClick(int newProgress);
	}

	//private Context mContext;
	private int mProgress = 0;// min 0; max: layout_width
	private int mMax = 100;

	// Attributes
	private int layout_width = 20;// the width of the view
	private int layout_height = 500;// the height of the view
	private int mBackgroundColor;
	private int mInnerColorPressed;
	private float mBackgroundWidth = 4f;// in dp
	private boolean mIsPressed;

	// Painters
	private Paint mBackgroundPainter;
	private Paint mBackgroundShadowPainter;
	private Paint mBackgroundInnerPainter;
	private Paint mImagePainer;

	private Bitmap mThumb;

	private SlimProgressClicked mISlimClicked;

	public int getBackgroundColor() {
		return mBackgroundColor;
	}

	public void setBackgroundColor(int mBackgroundColor) {
		this.mBackgroundColor = mBackgroundColor;
	}

	public float getBackgroundWidth() {
		return mBackgroundWidth;
	}

	public void setBackgroundWidth(float mBackgroundWidth) {
		this.mBackgroundWidth = mBackgroundWidth;
	}

	public SlimVerticalProgressBar(Context c) {
		super(c);
		//mContext = c;
		applySkin();
	}

	public SlimVerticalProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		//mContext = context;
		applySkin();
		initAttributes(context.obtainStyledAttributes(attrs,
				R.styleable.ArcProgressBar));
	}

	private void initAttributes(TypedArray tA) {
		mBackgroundColor = tA.getColor(
				R.styleable.ArcProgressBar_backgroundColor, mBackgroundColor);
		mBackgroundWidth = tA.getDimension(
				R.styleable.ArcProgressBar_backgroundWidth, mBackgroundWidth);
		tA.recycle();
	}

	private void applySkin() {
		mBackgroundColor = ThemeManager.getSelectedSkin()
				.getProgressVerticalBackgroundColor();
		mInnerColorPressed = ThemeManager.getSelectedSkin()
				.getProgressVerticalInnerColorPressed();
		mBackgroundWidth = ThemeManager.getSelectedSkin()
				.getProgressVerticalBackgroundWidth();
	}

	private void setupPainters() {
		mBackgroundPainter = new Paint();
		mBackgroundPainter.setColor(mBackgroundColor);
		mBackgroundPainter.setAntiAlias(true);
		mBackgroundPainter.setStyle(Style.STROKE);
		mBackgroundPainter.setStrokeWidth(mBackgroundWidth);
		mBackgroundPainter.setAlpha(50);

		mBackgroundInnerPainter = new Paint();
		mBackgroundInnerPainter.setColor(mInnerColorPressed);
		mBackgroundInnerPainter.setAntiAlias(true);
		mBackgroundInnerPainter.setStyle(Style.STROKE);
		mBackgroundInnerPainter.setStrokeWidth(mBackgroundWidth/2);

		mBackgroundShadowPainter = new Paint();
		mBackgroundShadowPainter.setColor(mBackgroundColor);
		mBackgroundShadowPainter.setAntiAlias(true);
		mBackgroundShadowPainter.setStyle(Style.STROKE);
		mBackgroundShadowPainter.setStrokeWidth(mBackgroundWidth * 2);
		mBackgroundShadowPainter.setAlpha(30);

		mImagePainer = new Paint();
		mImagePainer.setAntiAlias(true);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if (w > 64)
			w = 64;
		layout_width = w;
		layout_height = h;
		setupPainters();
		setThumb(false);
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// draw background shadow
		// canvas.drawArc(mCircleBounds, 180, 180, false,
		// mBackgroundShadowPainter);
		// draw the background
		canvas.drawLine(layout_width / 2, 0, layout_width / 2, layout_height,
				mBackgroundShadowPainter);
		if (mIsPressed) {
			canvas.drawLine(layout_width / 2, 2, layout_width / 2,
					layout_height-2, mBackgroundInnerPainter);
		}
		canvas.drawLine(layout_width / 2, 0, layout_width / 2, layout_height,
				mBackgroundPainter);

		int left = layout_width / 2 - mThumb.getWidth() / 2;
		int top = (mMax - mProgress) * layout_height / mMax
				- mThumb.getHeight() / 2;
		if (top < 0)
			top = 0;
		if (top > layout_height - mThumb.getHeight())
			top = layout_height - mThumb.getHeight();
		Log.d("lt", left + "," + top);
		canvas.drawBitmap(mThumb, left, top, mImagePainer);
		// canvas.drawPath(mProgressPath, mProgressPainter);
		// draw the progress
		// canvas.drawArc(mCircleBounds, 180, mProgress, false,
		// mProgressPainter);

		// PointF mPoint1 = new PointF(this.getWidth() / 2 - mImage.getWidth() /
		// 2, this.getHeight() - mImage.getHeight());
		// PointF mPoint2 = new PointF(this.getWidth() / 2,this.getHeight());
		// Path myPath1 = new Path();
		// Paint paint = new Paint();
		// paint.setAntiAlias(true);
		// paint.setStyle(Style.STROKE);
		// paint.setStrokeWidth(2);
		// paint.setColor(Color.WHITE);
		//
		// myPath1 = drawCurve(canvas, paint, mPoint1, mPoint2);
		// canvas.drawPath(myPath1, paint);

	}

	// private Path drawCurve(Canvas canvas, Paint paint, PointF mPointa, PointF
	// mPointb) {
	//
	// Path myPath = new Path();
	// myPath.moveTo(63*1200/64, 768/10);
	// myPath.quadTo(mPointa.x, mPointa.y, mPointb.x, mPointb.y);
	// return myPath;
	// }
	//

	/**
	 * @param progress
	 *            : The progress of task. From 0 to 100
	 */
	public synchronized void setProgress(int progress) {
		if (progress < 0)
			progress = 0;
		if (progress > mMax)
			progress = mMax;
		this.mProgress = progress;
		invalidate();
	}

	public int getProgress() {
		return mProgress;
	}

	public int getMax() {
		return mMax;
	}

	public void setMax(int max) {
		this.mMax = max;
	}

	public void setOnSlimClicked(SlimProgressClicked click) {
		this.mISlimClicked = click;
	}

	public void setThumb(boolean isPressed) {
		Drawable drawable = null;
		if (!isPressed) {
			drawable = ThemeManager.getSelectedSkin()
					.getProgressVerticalThumbNormal();
		} else {
			drawable = ThemeManager.getSelectedSkin()
					.getProgressVerticalThumbPressed();
		}
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		if (w > layout_width)
			w = layout_width;
		if (h > layout_width * 2) {
			h = layout_width * 2;
		}
		Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
		drawable.draw(canvas);
		mThumb = bitmap;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int progress = this.mProgress;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setThumb(true);
			progress = (int) ((layout_height - event.getY()) * mMax / layout_height);
			setProgress(progress);
			mISlimClicked.onClick(this.mProgress);
			mIsPressed = true;
			invalidate();
			return true;
		case MotionEvent.ACTION_MOVE:
			setThumb(true);
			progress = (int) ((layout_height - event.getY()) * mMax / layout_height);
			if (progress > mMax || progress < 0)
				return true;
			setProgress(progress);
			// Log.d("progress", progress + "");
			mISlimClicked.onClick(this.mProgress);
			mIsPressed = true;
			invalidate();
			return true;
		case MotionEvent.ACTION_UP:
			setThumb(false);
			mIsPressed = false;
			invalidate();
			break;
		}
		return true;
	}
}
